﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;

namespace Communicate
{
    public class lCom
    {
        public  SerialPort mySerialPort = new SerialPort();
        public event EventHandler DataReceived;
        public int BaudRate
        {
            get
            {
                return this.mySerialPort.BaudRate;
            }
            set
            {
                this.mySerialPort.BaudRate = value;
            }
        }
        public int DataBits
        {
            get
            {
                return this.mySerialPort.DataBits;
            }
            set
            {
                this.mySerialPort.DataBits = value;
            }
        }
        public bool DtrEnable
        {
            get
            {
                return this.mySerialPort.DtrEnable;
            }
            set
            {
                this.mySerialPort.DtrEnable = value;
            }
        }
        public Handshake Handshake
        {
            get
            {
                return this.mySerialPort.Handshake;
            }
            set
            {
                this.mySerialPort.Handshake = value;
            }
        }
        public bool IsOpen
        {
            get
            {
                return this.mySerialPort.IsOpen;
            }
        }
        public Parity Parity
        {
            get
            {
                return this.mySerialPort.Parity;
            }
            set
            {
                this.mySerialPort.Parity = value;
            }
        }
        public string PortName
        {
            get
            {
                return this.mySerialPort.PortName;
            }
            set
            {
                this.mySerialPort.PortName = value;
            }
        }
        public int ReadTimeout
        {
            get
            {
                return this.mySerialPort.ReadTimeout;
            }
            set
            {
                this.mySerialPort.ReadTimeout = value;
            }
        }
        public bool RtsEnable
        {
            get
            {
                return this.mySerialPort.RtsEnable;
            }
            set
            {
                this.mySerialPort.RtsEnable = value;
            }
        }
        public StopBits StopBits
        {
            get
            {
                return this.mySerialPort.StopBits;
            }
            set
            {
                this.mySerialPort.StopBits = value;
            }
        }
        public lCom()
        {            
            this.ReadTimeout = 15000;
            this.RtsEnable = true;
            this.mySerialPort.DataReceived += new SerialDataReceivedEventHandler(this.sp_DataReceived);
        }
        public lCom(string comPort, int baudRate)
        {
            this.PortName = comPort;
            this.BaudRate = baudRate;
            this.ReadTimeout = 15000;
            this.RtsEnable= true;
            this.mySerialPort.DataReceived += new SerialDataReceivedEventHandler(this.sp_DataReceived);
        }
        private void sp_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            this.OnDataReceived(e);
        }
        protected virtual void OnDataReceived(EventArgs e)
        {
            if (this.DataReceived != null)
            {
                this.DataReceived(this, EventArgs.Empty);
            }
        }
        public bool Close(out string strRel)
        {
            strRel = "串口已关闭";
            try
            {
                if (this.mySerialPort.IsOpen)
                {
                    this.mySerialPort.Close();
                }
                return true;
            }
            catch (Exception ex)
            {
                strRel = "串口关闭失败！\r\n原因：" + ex.ToString();
                return false;
            }

            
        }
        public void DiscardInBuffer()
        {
            this.mySerialPort.DiscardInBuffer();
        }
        public void DidcardOutBuffer()
        {
            this.mySerialPort.DiscardOutBuffer();
        }
        public bool Open(out string strRel)
        {
            bool result;
            strRel = "串口已打开";
            try
            {
                this.mySerialPort.Open();
                result = true;
                return result;
            }
            catch (Exception ex)
            {
                strRel="串口打开失败！\r\n原因："+ex.ToString();
                result = false;
                return result;
            }
        }
        public int ReadByte()
        {
            return this.mySerialPort.ReadByte();
        }
        public int ReadChar()
        {
            return this.mySerialPort.ReadChar();
        }
        public string ReadExisting()
        {
            string sResult = string.Empty;
            try
            {
                sResult = this.mySerialPort.ReadExisting();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return sResult;
        }
        public string ReadLine()
        {
            string sResult = string.Empty;
            try
            {
                sResult = this.mySerialPort.ReadLine();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return sResult;
        }
        public string ReadTo(string value)
        {
            return this.mySerialPort.ReadTo(value);
        }
        public void Write(string text)
        {
            this.mySerialPort.Write(text);
        }
        public void Write(byte[] buffer, int offset, int count)
        {
            this.mySerialPort.Write(buffer, offset, count);
        }
        public void WriteLine(string text)
        {
            this.mySerialPort.WriteLine(text);
        }
    }
}
